package org.smarthings.things.management.impl;

import org.smarthings.things.management.Thing;
import org.smarthings.things.management.ThingProperty;
import org.smarthings.things.management.metadata.ThingPropertyMetadata;

public class ThingPropertyImpl implements ThingProperty {

	private String UUID;
	private String type;
	private Object value;

	private Thing thing;
	private ThingsEventPropagator thingsEventPropagator;
	private ThingPropertyMetadata metadata;

	public ThingPropertyImpl(String type, ThingPropertyMetadata metadata,
			ThingsEventPropagator thingsEventPropagator) {
		this.type = type;
		this.metadata = metadata;
		this.thingsEventPropagator = thingsEventPropagator;
		this.UUID = generateThingPropertyUUID();
	}

	@Override
	public String getUUID() {
		return UUID;
	}

	@Override
	public String getName() {
		return type;
	}
	
	@Override
	public String getType() {
		return type;
	}

	@Override
	public Object getValue() {
		return value;
	}

	@Override
	public void setValue(Object value) throws Exception {
		this.value = value;
		this.thingsEventPropagator.fireThingPropertyChangedEvent(thing, this);

	}

	@Override
	public ThingPropertyMetadata getMetadata() {
		return metadata;
	}

	void bindThing(Thing thing) {
		this.thing = thing;
	}

	private static long index = 0;

	private static String generateThingPropertyUUID() {
		return String.valueOf(++index);
	}
}
